package com.replication.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

import com.replication.logging.Logger;

public class Statements {
	
	static final Logger LOGGER = Logger.getLogger(Statements.class);
	
    protected static Statement createStatement(Connection conn,Integer resultSetType, Integer resultSetConcurrency,Integer resultSetHoldability) throws SQLException{
        if(resultSetType!= null && resultSetConcurrency!=null && resultSetHoldability!=null){
            return conn.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
        }
        if(resultSetType!= null && resultSetConcurrency!=null){
            return conn.createStatement(resultSetType, resultSetConcurrency);
        }
        
        return conn.createStatement();
    }
    
    protected static Statement createStatement(ReplicationConnectionProxy conn,String sql,StatementOpts opts,Integer resultSetType, Integer resultSetConcurrency,Integer resultSetHoldability) throws SQLException{
        Statement result = null;
        result = createStatement(getTargetConnection(conn,sql),resultSetType,resultSetConcurrency,resultSetHoldability);
        opts.bound(result);
        return result;
    }
    
    protected static Statement createStatementOnMaster(ReplicationConnectionProxy conn,StatementOpts opts,Integer resultSetType, Integer resultSetConcurrency,Integer resultSetHoldability) throws SQLException{      
        Statement result = createStatement(conn.getMasterConnection(),resultSetType,resultSetConcurrency,resultSetHoldability);
        opts.bound(result);
        return result;
    }
    
    protected static Connection getTargetConnection(ReplicationConnectionProxy conn,String sql) throws SQLException{
    	Connection c =  conn.getConnectionSelector().select(conn,sql);
        LOGGER.debug("{selector url}[" + c.getMetaData().getURL()+"]" + sql);
        return c;
    }
    
    protected static PreparedStatement prepareStatement(ReplicationConnectionProxy conn,String sql) throws SQLException{
        PreparedStatement result = null;      
        result = getTargetConnection(conn,sql).prepareStatement(sql);
        return result;
    }
    
    protected static PreparedStatement prepareStatement(ReplicationConnectionProxy conn,String sql, int autoGeneratedKeys) throws SQLException {
        return getTargetConnection(conn,sql).prepareStatement(sql, autoGeneratedKeys);
    }


    protected static PreparedStatement prepareStatement(ReplicationConnectionProxy conn,String sql, int[] columnIndexes) throws SQLException {
        return getTargetConnection(conn,sql).prepareStatement(sql, columnIndexes);
    }


    protected static PreparedStatement prepareStatement(ReplicationConnectionProxy conn,String sql, String[] columnNames) throws SQLException {
        return getTargetConnection(conn,sql).prepareStatement(sql, columnNames);
    }


    protected static PreparedStatement prepareStatement(ReplicationConnectionProxy conn,String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
        return getTargetConnection(conn,sql).prepareStatement(sql, resultSetType, resultSetConcurrency);
    }


    protected static PreparedStatement prepareStatement(ReplicationConnectionProxy conn,String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
        return getTargetConnection(conn,sql).prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
    }
}
